package com.hc.oa.demo.service;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.hc.oa.demo.dao.SlMenuDao;
import com.hc.oa.model.SlMenu;

///

@Component
public class MenuServices {
	
	@Resource
	private SlMenuDao slmenuDao;
	
	@DataProvider
	public Collection<SlMenu> getTopMenu()
	{
		return slmenuDao.find("from SlMenu where slMenu.menuId is null");
	}
	
	@DataProvider
	public  Collection<SlMenu> getMenuByParentId(Integer Parentid) 
	{
		if(null!=Parentid)
		{
			String hql="from SlMenu where slMenu.menuId =:menuId";
			Map paramMap = new HashMap();
			paramMap.put("menuId",Parentid);
			return slmenuDao.find(hql, paramMap);
		}else {
			return null;
		}
	}
	
	@DataResolver
	@Transactional
	public void saveAll(Collection<SlMenu> slmenus)
	{
		for(SlMenu menu:slmenus)
		{
			slmenuDao.persistEntity(menu);
			Collection<SlMenu> childSlMenus = menu.getSlMenuSet();
			if(childSlMenus !=null)
			{
				for(SlMenu child:childSlMenus)
				{
					child.setSlMenu(menu);
				}
			slmenuDao.persistEntities(childSlMenus);
			saveAll(childSlMenus);
			}
		}
	}
	

}
